﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using KPIS.GERP.GHRMS.DAL;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.GERP.GHRMS.IDAL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.DALFactory;

namespace KPIS.GERP.GHRMS.BLL
{
    public class PositionBLL : IPositionBLL
    {
        private static readonly IPositionDAL dal = DataAccess.CreatePositions();

        private FieldInfo fieldInfo = new FieldInfo();
        private ExecutiveInfo execInfo = new ExecutiveInfo();
        private EmployeeTypeInfo etInfo = new EmployeeTypeInfo();
        private JobSiteInfo jsInfo = new JobSiteInfo();
        private PositionTypeInfo ptInfo = new PositionTypeInfo();
        private ClassSpecificationInfo specInfo = new ClassSpecificationInfo();
        private PositionStatusInfo psInfo = new PositionStatusInfo();
        private PositionInfo upperInfo = new PositionInfo();
        private PositionInfo workingInfo = new PositionInfo();
        private PositionInfo posInfo = new PositionInfo();

        public DataTable ListPositionsNew()
        {
            return dal.ListPositions();
        }
        public IList<PositionInfo> ListPositions()
        {
            return dal.ListPositions(new PositionInfo(0));
        }
        public IList<PositionInfo> ListPositions(string JobSite)
        {
            if (JobSite.Length > 0)
                jsInfo.Sequence = Int32.Parse(JobSite);
            
            posInfo.JobSite = jsInfo;
            return dal.ListPositions(posInfo);
        }
        public IList<PositionInfo> ListPositions(string JobSite, string EmployeeType)
        {
            if (JobSite.Length > 0)
            {
                jsInfo.Sequence = Int32.Parse(JobSite);
                posInfo.JobSite = jsInfo;
            }
            if (EmployeeType.Length > 0)
            {
                etInfo.Sequence = Int32.Parse(EmployeeType);
                posInfo.EmployeeType = etInfo;
            }
            
            return dal.ListPositions(posInfo);
        }
        public IList<PositionInfo> ListPositionsBySequence(string Sequence)
        {
            int seq = Sequence.Length > 0 ? Int32.Parse(Sequence) : 0;
            return dal.ListPositions(new PositionInfo(seq));
        }
        public IList<PositionInfo> LoadPositionTreeView(Nullable<int> EmpTypeSeq)
        {
            return dal.LoadPositionTreeView(EmpTypeSeq);
        }
        public IList<PositionInfo> LoadWorkingTreeView(Nullable<int> EmpTypeSeq)
        {
            return dal.LoadWorkingTreeView(EmpTypeSeq);
        }
        public IList<TreeViewInfo> LoadPositionJobsites(Nullable<int> EmpTypeSeq, string ParentID)
        {
            return dal.LoadPositionJobsites(EmpTypeSeq, ParentID);
        }
        public EmployeeInfo LoadEmployeeOfPosition(int positionSequence)
        {
            return dal.LoadEmployeeOfPosition(positionSequence);
        }
        public EmployeeInfo LoadEmployeeOfPosition(string positionSequence)
        {
            return !String.IsNullOrEmpty(positionSequence) ? dal.LoadEmployeeOfPosition(Int32.Parse(positionSequence)) : null;
        }

        public DataTable LoadPositionWorkingJobsites(Nullable<int> EmpTypeSeq, string ParentID)
        {
            return dal.LoadPositionWorkingJobsites(EmpTypeSeq, ParentID);
        }
        public DataSet LoadPositions(Nullable<int> Sequence, Nullable<int> EmployeeType, Nullable<int> Jobsite, string SearchText)
        {
            if (Sequence.HasValue)
                posInfo.Sequence = Sequence;
            if (Jobsite.HasValue)
            {
                jsInfo.Sequence = Jobsite;
                posInfo.JobSite = jsInfo;
            }
            if (EmployeeType.HasValue)
            {
                etInfo.Sequence = (int)EmployeeType;
                posInfo.EmployeeType = etInfo;
            }
            posInfo.SearchText = SearchText;

            return dal.LoadPositions(posInfo);
        }

		public Dictionary<string, string> GetJobSiteAndPositionTypeAndClassByPosition(string positionSeq) 
		{
			return dal.GetJobSiteAndPositionTypeAndClassByPosition(positionSeq);
		}

        public void Insert(string RefNo, string Name, string JobDescription, Nullable<DateTime> DefinedDate
            , Nullable<DateTime> MoneySupportDate, Nullable<double> SalaryQuota, Nullable<double> SalaryMax
            , Nullable<double> SalaryMin, string Field, string Executive, string EmployeeType
            , string JobSite, string PositionType, string Upper, string WorkingUpper, string ClassSpecification
            , string Class, string Status, string Remarks, int CreatedBy) //
        {
            if (Field.Length > 0)
                fieldInfo.Sequence = Int32.Parse(Field);

            if (Executive.Length > 0)
                execInfo.Sequence = Int32.Parse(Executive);
                
            if (EmployeeType.Length > 0)
                etInfo.Sequence =  Int32.Parse(EmployeeType);

            if (JobSite.Length > 0)
                jsInfo.Sequence =  Int32.Parse(JobSite);

            if (PositionType.Length > 0)
                ptInfo.Sequence = Int32.Parse(PositionType);

			if (!string.IsNullOrEmpty(ClassSpecification))
                specInfo.Sequence = Int32.Parse(ClassSpecification);

			if (!string.IsNullOrEmpty(Status))
                psInfo.Sequence = Int32.Parse(Status);

            if (Upper.Length > 0)
                upperInfo.Sequence = Int32.Parse(Upper);

            //if (WorkingUpper.Length > 0)
            if (WorkingUpper != null)
                workingInfo.Sequence = Int32.Parse(WorkingUpper);           

            try
            {
                var convertNull = new ConvertNullable();
                dal.Insert(new PositionInfo(0, RefNo, Name, JobDescription, DefinedDate, MoneySupportDate, SalaryQuota
                    , SalaryMax, SalaryMin, fieldInfo, execInfo, etInfo, jsInfo, ptInfo, upperInfo, workingInfo
                    , specInfo, null, psInfo, Remarks, "", CreatedBy, 0)
                    {
                        ClassSeq = convertNull.ConvertToInt(Class)
                    }); 
            }
            catch (Exception ex)
            {
                //throw new Exception("");
            }
        }

        public void Update(int Sequence, string RefNo, string Name, string JobDescription, Nullable<DateTime> DefinedDate
            , Nullable<DateTime> MoneySupportDate, Nullable<double> SalaryQuota, Nullable<double> SalaryMax
            , Nullable<double> SalaryMin, string Field, string Executive, string EmployeeType
            , string JobSite, string PositionType, string Upper, string WorkingUpper, string ClassSpecification, string Class
            , string Status, string Remarks, int UpdatedBy) //
        {
            if (Field.Length > 0)
                fieldInfo.Sequence = Int32.Parse(Field);

            if (Executive.Length > 0)
                execInfo.Sequence = Int32.Parse(Executive);

            if (EmployeeType.Length > 0)
                etInfo.Sequence = Int32.Parse(EmployeeType);

            if (JobSite.Length > 0)
                jsInfo.Sequence = Int32.Parse(JobSite);

            if (PositionType.Length > 0)
                ptInfo.Sequence = Int32.Parse(PositionType);

			if (!string.IsNullOrEmpty(ClassSpecification))
                specInfo.Sequence = Int32.Parse(ClassSpecification);

            if (!string.IsNullOrEmpty(Status))
                psInfo.Sequence = Int32.Parse(Status);

            if (Upper.Length > 0)
                upperInfo.Sequence = Int32.Parse(Upper);

            //if (WorkingUpper.Length > 0)
            if (WorkingUpper != null)
                workingInfo.Sequence = Int32.Parse(WorkingUpper); 

            try
            {
	            var convertNull = new ConvertNullable();
                dal.Update(new PositionInfo(Sequence, RefNo, Name, JobDescription, DefinedDate, MoneySupportDate, SalaryQuota
                    , SalaryMax, SalaryMin, fieldInfo, execInfo, etInfo, jsInfo, ptInfo, upperInfo, workingInfo
					, specInfo, null, psInfo, Remarks, "", 0, UpdatedBy) { ClassSeq = convertNull.ConvertToInt(Class) }); //
            }
            catch
            {
                throw new Exception("");
            }
        }
        public void Delete(int Sequence, int UpdatedBy)
        {
            try
            {
                dal.Delete(Sequence, UpdatedBy);
            }
            catch
            {
                throw new Exception("");
            }
        }
        public void UpdateUpper(int Sequence, string Upper, int UpdatedBy)
        {
            try
            {
                dal.UpdateUpper(Sequence, Upper, UpdatedBy);
            }
            catch
            {
                throw new Exception("");
            }
        }
        public void UpdateWorkingUpper(int Sequence, string WorkingUpper, int UpdatedBy)
        {
            try
            {
                dal.UpdateWorkingUpper(Sequence, WorkingUpper, UpdatedBy);
            }
            catch
            {
                throw new Exception("");
            }
        }
        public void UpdateJobSites(int Sequence, string JobSiteSeq, int UpdatedBy)
        {
            try
            {
                dal.UpdateJobSites(Sequence, JobSiteSeq, UpdatedBy);
            }
            catch
            {
                throw new Exception("");
            }
        }

        public List<WorkOnDuty> SelectWorkOnDuty()
        {
            return dal.SelectWorkOnDuty();
        }

        public WorkOnDuty SelectWorkOnDutyById(int wodId)
        {
            return dal.SelectWorkOnDuty().FirstOrDefault(o => o.wod_seq == wodId);
        }

        public WorkOnDuty InsertWorkOnDuty(WorkOnDuty word)
        {
            return dal.InsertWorkOnDuty(word);
        }

        public WorkOnDuty UpdateWorkOnDuty(WorkOnDuty word)
        {
            return dal.UpdateWorkOnDuty(word);
        }

        public WorkOnDuty DeleteWorkOnDuty(WorkOnDuty word)
        {
            return dal.DeleteWorkOnDuty(word);
        }
    }
}
